﻿using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZhuXingFamilys.AuditIndicators.utils
{
    /// <summary>
    /// 楼层平面工具
    /// </summary>
    public class ViewPlaneUtils
    {
        /// <summary>
        /// 通过高度创建楼层平面
        /// </summary>
        public static ViewPlan CreateViewPlaneByHeight(Document doc, double height)
        {
            //创建标高
            Level level = Level.Create(doc, height);

            //获取视图类型ID
            ViewFamilyType viewFamilyType = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType)).Cast<ViewFamilyType>().FirstOrDefault(x => ViewFamily.FloorPlan == x.ViewFamily);
            ElementId viewFamilyTypeId = viewFamilyType.Id;

            //创建视图
            ViewPlan viewPlane = ViewPlan.Create(doc, viewFamilyType.Id, level.Id);
            //修改截裁剪为裁剪时有截面线(0不裁剪，1裁剪时有截面线，2裁剪时无截面线)
            Parameter clipParam = viewPlane.get_Parameter(BuiltInParameter.VIEW_BACK_CLIPPING);
            clipParam.Set(1);
            //修改详细程度为精细
            viewPlane.DetailLevel = ViewDetailLevel.Coarse;
            //修改显示模式为线框
            viewPlane.DisplayStyle = DisplayStyle.HLR;
            //获取视图范围
            PlanViewRange planViewRange = viewPlane.GetViewRange();
            //修改视图范围参数
            planViewRange.SetOffset(PlanViewPlane.TopClipPlane, 500 / 304.8);//顶部
            planViewRange.SetOffset(PlanViewPlane.CutPlane, 500 / 304.8);//剖切面
            planViewRange.SetOffset(PlanViewPlane.BottomClipPlane, 0);//底部
            planViewRange.SetOffset(PlanViewPlane.ViewDepthPlane, 0);//视图深度
            //应用设置好的视图范围
            viewPlane.SetViewRange(planViewRange);

            return viewPlane;
        }

        /// <summary>
        /// 在视图标高上绘制线，注意线应在视图工作平面上
        /// </summary>
        private static List<ModelCurve> CreateModeLinesInView(Document doc, List<Curve> curves, View view)
        {
            List<ModelCurve> resultCurves = new List<ModelCurve>();
            foreach (Curve c in curves)
            {
                resultCurves.Add(doc.Create.NewModelCurve(c, view.SketchPlane));
            }
            return resultCurves;
        }
    }
}
